/* GUI CONVERTED from rssupa.p (converter v1.76) Sun Feb 24 20:08:41 2002 */
/* rssupa.p - Release Management Supplier Schedules                           */
/* Copyright 1986-2002 QAD Inc., Carpinteria, CA, USA.                        */
/* All rights reserved worldwide.  This is an unpublished work.               */
/* $Revision: 1.8.1.11 $                                                       */
/* REVISION: 7.3      LAST MODIFIED: 12/14/92   BY: WUG *G462*                */
/* REVISION: 7.3      LAST MODIFIED: 06/17/93   BY: WUG *GC40*                */
/* REVISION: 7.3      LAST MODIFIED: 07/14/93   BY: WUG *GD42*                */
/* REVISION: 7.3      LAST MODIFIED: 11/02/93   BY: WUG *GG88*                */
/* REVISION: 8.5      LAST MODIFIED: 08/09/94   BY: tjs *J014*                */
/* REVISION: 7.3      LAST MODIFIED: 10/31/94   BY: WUG *GN76*                */
/* REVISION: 7.3      LAST MODIFIED: 11/01/94   BY: ame *GN88*                */
/* REVISION: 7.4      LAST MODIFIED: 02/11/95   BY: ljm *G0DZ*                */
/* REVISION: 7.3      LAST MODIFIED: 03/29/95   BY: dzn *F0PN*                */
/* REVISION: 7.3      LAST MODIFIED: 05/15/95   BY: WUG *G0MV*                */
/* REVISION: 7.3      LAST MODIFIED: 08/29/95   BY: vrn *G0VT*                */
/* REVISION: 8.5      LAST MODIFIED: 06/14/96   BY: ruw *J0TK*                */
/* REVISION: 8.5      LAST MODIFIED: 08/06/96   BY: *G2BF* Sanjay Patil       */
/* REVISION: 8.5      LAST MODIFIED: 04/23/97   BY: *H0WH* Aruna Patil        */
/* REVISION: 8.6E     LAST MODIFIED: 02/23/98   BY: *L007* A. Rahane          */
/* REVISION: 8.6E     LAST MODIFIED: 05/20/98   BY: *K1Q4* Alfred Tan         */
/* REVISION: 8.6E     LAST MODIFIED: 10/04/98   BY: *J314* Alfred Tan         */
/* REVISION: 9.1      LAST MODIFIED: 11/17/99   BY: *N04H* Vivek Gogte        */
/* REVISION: 9.1      LAST MODIFIED: 04/11/00   BY: *J3PK* Reetu Kapoor       */
/* REVISION: 9.1      LAST MODIFIED: 07/25/00   BY: *N0GQ* Mudit Mehta        */
/* Old ECO marker removed, but no ECO header exists *GVRN*                    */
/* Revision: 1.8.1.9  BY: Kaustubh K.           DATE: 05/27/01 ECO: *M18L*    */
/* $Revision: 1.8.1.11 $     BY: Robin McCarthy   DATE: 10/01/01 ECO: *P025*    */
/* $Revision: 1.8.1.11 $     BY: Anitha Gopal     DATE: 02/17/02 ECO: *N19G*    */

/******************************************************************************/
/* All patch markers and commented out code have been removed from the source */
/* code below. For all future modifications to this file, any code which is   */
/* no longer required should be deleted and no in-line patch markers should   */
/* be added.  The ECO marker should only be included in the Revision History. */
/******************************************************************************/

/* SUPPLIER SCHEDULE UPDATE SUBPROGRAM */

/* THIS PROGRAM LOOKS AT MRP PLANNED ORDERS AND SELECTS BASED ON "ARRIVAL time*/

/*V8:ConvertMode=Report                                                       */


/*GUI preprocessor directive settings */
&SCOPED-DEFINE PP_GUI_CONVERT_MODE REPORT

{mfdeclre.i}
{gplabel.i} /* EXTERNAL LABEL INCLUDE */

/* ********** Begin Translatable Strings Definitions ********* */

&SCOPED-DEFINE rssupa_p_9 "Qty Ordered x!Alloc %"
/* MaxLen: Comment: */

&SCOPED-DEFINE rssupa_p_10 "Ship/Dlvy!Adj. Date"
/* MaxLen: Comment: */

&SCOPED-DEFINE rssupa_p_12 "Schedule!Open Qty"
/* MaxLen: Comment: */

&SCOPED-DEFINE rssupa_p_13 "Reduction Qty"
/* MaxLen: Comment: */

&SCOPED-DEFINE rssupa_p_15 "Safety LT!Adj. Date"
/* MaxLen: Comment: */

&SCOPED-DEFINE rssupa_p_17 "Calendar!Adj. Date"
/* MaxLen: Comment: */

&SCOPED-DEFINE rssupa_p_18 "Alloc %"
/* MaxLen: Comment: */

/* ********** End Translatable Strings Definitions ********* */

/* INPUT PARAMS */
define input        parameter pod_recid       as recid.
define input        parameter start_date      as date.
define input        parameter asof_date       as date.
define input        parameter report_yn       like mfc_logical.
define input-output parameter warn_ct         as integer no-undo.

/* OUTPUT PARAMS */
define shared workfile work_schd like schd_det.

define           variable work_qty            like wo_qty_ord.
define           variable shipfrom_date       as date.
define           variable shipfrom_time       as character.
define           variable arrive_date         as date.
define           variable arrive_time         as character.
define           variable ret_status          as character.
define           variable ship_dlvy_date      as date.
define           variable safety_date         as date.
define           variable alloc_percent       as decimal
   label {&rssupa_p_18} format ">>9".
define           variable supp_schedule       as integer initial 4.
define           variable open_qty            as decimal.
define           variable qty_left            as decimal.
define           variable qty_to_apply        as decimal.
define           variable match_wr            as logical.
define           variable routing             as character.
define workfile work_wr_route no-undo like wr_route.

define            variable i                  as integer   no-undo.
define            variable wotype             like wo_type no-undo.
define            variable wopart             like wo_part no-undo.
define            variable header_desc        as character no-undo.
define            variable l_tmp_string1      as character no-undo.
define            variable l_tmp_string2      as character no-undo.

define new shared variable ship_dlvy_code     as character.
define new shared variable interval_code      as character.
define new shared variable req_dt             as date.
define new shared variable req_qty            as decimal.
define new shared variable week_offset        as integer.
define new shared variable shipto_calid       as character.

define new shared variable resch_ct           as integer.
define new shared variable resch_dt           as date    extent 999 no-undo.
define new shared variable resch_qty          as decimal extent 999 no-undo.
define new shared variable working_hours      as decimal extent 999 no-undo.
define new shared variable resch_stat         as character.

define new shared variable wo_recno           as recid.
define new shared variable l_inc_wkend_shpcal like so_incl_iss
   no-undo.
define            variable l-msg-planned       as character no-undo
   format "x(80)".
define            variable l-msg-order-not-sel as character no-undo
   format "x(30)".
define            variable l-msg-opr-data      as character no-undo
   format "x(65)".
define            variable l-msg-opr-data-open as character no-undo
   format "x(100)".
define            variable l-msg-opr-data-del as character no-undo
   format "x(65)".
define            variable l-msg-order-data   as character no-undo
   format "x(50)".

for each work_schd exclusive-lock:
   delete work_schd.
end.

find pod_det where recid(pod_det) = pod_recid no-lock.
find po_mstr where po_nbr = pod_nbr no-lock.

if pod_type = "" then do:
   /* GET MRP PLANNED ORDER DATA AND DROP INTO SCHEDULE */

   for each wo_mstr no-lock
       where wo_part       = pod_part
         and wo_site       = pod_site
         and wo_status     = "P"
         and wo_joint_type = ""
         use-index wo_part
         by wo_due_date:
   /*  Get the latest date of allocation on or before the wo_due_date */
   /*  with same item and site combination on current P0 for any PO   */

      assign
         alloc_percent = 0
         l_tmp_string1 =   string(pod_site, "x(8)")
                         + string(pod_part, "x(18)")
         l_tmp_string2 = l_tmp_string1
            + string(year(max(wo_due_date,asof_date)),"9999")
            + string(month(max(wo_due_date,asof_date)),"99")
            + string(day(max(wo_due_date,asof_date)),"99")
            + pod_nbr.

      find last qad_wkfl where qad_key1 = "poa_det"
                           and qad_key2 >= l_tmp_string1
                           and qad_key2 <= l_tmp_string2
         no-lock no-error.

      if available qad_wkfl then do:

         /* Check whether there exist a allocation percentage for */
         /* current purchase order on latest allocation for site  */
         /* and item combination                                  */

         l_tmp_string2  =  l_tmp_string1
                         + substring(qad_key2,27,8)
                         + pod_nbr.

         find qad_wkfl where qad_key1 = "poa_det"
                         and qad_key2 = l_tmp_string2 no-lock no-error.
         if available qad_wkfl then
            alloc_percent = qad_decfld[1].
         else alloc_percent = 0.
      end.
      if alloc_percent = 0 then
         next. /* wo_mstr */

      /* BACK OFF FOR SAFETY LEADTIME */

      assign
         safety_date    = wo_due_date - pod_sftylt_days
         ship_dlvy_code = pod_sd_pat
         /* INTERPRET THE DUE DATE PER THE SHIP/DELIVERY PATTERN CODE */
         interval_code  = "D"
         req_dt         = safety_date
         req_qty        = 0.  /* THIS DOESN'T MATTER SINCE DISCRETE */
      {gprun.i ""rcsdpint.p""}

      if resch_stat <> "" then do:
         warn_ct = warn_ct + 1.

         put unformatted skip(2)
            caps(getTermLabel("WARNING",10)) + ": " + resch_stat
            skip(1).
      end.

      ship_dlvy_date = resch_dt[1].

      /* GET THE ARRIVAL DATE WHICH IS DETERMINED BY LOOKING AT THE
      SHIP-FROM AND SHIP-TO SITE CALENDARS (IF THERE ARE ANY).  WE
      ALSO USE THE TRANSPORTATION LEADTIME IF THERE IS ANY SO THAT
      WHEN WE TELL THEM TO SHIP, BOTH PARTIES WILL BE OPERATING . */

      {gprun.i ""rcshipdt.p""
                "(input po_vend,
                  input pod_site,
                  input ship_dlvy_date,
                  input """",
                  input pod_translt_days,
                  output shipfrom_date,
                  output shipfrom_time,
                  output arrive_date,
                  output arrive_time,
                  output ret_status)"}

      if ret_status <> "" then do:
         warn_ct = warn_ct + 1.
         put unformatted skip(2)
            caps(getTermLabel("WARNING",10)) + ":" space(1) ret_status
            wo_due_date
            skip(1).
      end.

      {pxmsg.i
         &MSGNUM = 3645
         &MSGBUFFER = l-msg-planned
         }
      {pxmsg.i
         &MSGNUM = 3650
         &ERRORLEVEL = 2
         &MSGBUFFER = l-msg-order-not-sel
         }
      {pxmsg.i
         &MSGNUM = 3646
         &MSGBUFFER = l-msg-opr-data
         }
      {pxmsg.i
         &MSGNUM = 3647
         &MSGBUFFER = l-msg-opr-data-open
         }
      {pxmsg.i
         &MSGNUM = 3648
         &MSGBUFFER = l-msg-opr-data-del
         }
      {pxmsg.i
         &MSGNUM = 3649
         &ERRORLEVEL = 2
         &MSGBUFFER = l-msg-order-data
         }

      FORM /*GUI*/  header
         skip(2)
         l-msg-planned + " " + string(start_date) + " " format "x(90)"
         skip(1)
      with STREAM-IO /*GUI*/  frame hdr-planned width 132 page-top no-box.

      view frame hdr-planned.

      assign
         work_qty = wo_qty_chg * (alloc_percent / 100)
         work_qty = work_qty / pod_um_conv
         work_qty = truncate(work_qty + .9999999999,0).

      do with frame f-d width 132 no-box down:
         setFrameLabels(frame f-d:handle).
         if arrive_date < start_date then do:
            warn_ct = warn_ct + 1.

            display
               wo_nbr
               wo_lot
               wo_qty_chg @ wo_qty_ord
               wo_due_date
               safety_date    column-label {&rssupa_p_15}
               ship_dlvy_date column-label {&rssupa_p_10}
               arrive_date    column-label {&rssupa_p_17}
               alloc_percent
               work_qty       column-label {&rssupa_p_9}
               l-msg-order-not-sel no-label WITH STREAM-IO /*GUI*/ .
         end.
         else
         if (arrive_date >= start_date or start_date = ?)
         then do:

            if work_qty <> 0 then do:
               create work_schd.
               assign
                  schd_nbr = pod_nbr
                  schd_line = pod_line
                  schd_date = arrive_date
                  schd_time = arrive_time
                  schd_discr_qty = work_qty
                  schd__chr01 = wo_lot.
            end.

            if report_yn then do:
               display
                  wo_nbr
                  wo_lot
                  wo_qty_chg @ wo_qty_ord
                  wo_due_date
                  safety_date    column-label {&rssupa_p_15}
                  ship_dlvy_date column-label {&rssupa_p_10}
                  arrive_date    column-label {&rssupa_p_17}
                  alloc_percent
                  work_qty       column-label {&rssupa_p_9} WITH STREAM-IO /*GUI*/ .
            end.
         end.
      end. /* do with frame f-d */
   end.
end.

/* Added revised front end for subcontract orders                    */
/*                                                                   */
/* Notes regarding this application:                                 */
/*   - Processing for subcontract schedules is nearly completely     */
/*     different than it is for regular schedules                    */
/*   - This process evaluates planned orders AND scheduled orders    */
/*     Because there may be multiple subcontract operations for an   */
/*     order, the total percentage allocated for an order may be     */
/*     greater than 100%                                             */
/*   - MRP planned orders are not deleted after quantities have been */
/*     distributed to subcontract supplier schedules                 */
/*                                                                   */

else if (pod_type = "s" and pod_wo_lot <> "") then do:

   /* Determine the item number for this PO line (pod_det)           */
   wopart = "".
   find wo_mstr where wo_lot = pod_wo_lot no-lock no-error.
   if available wo_mstr then do:
      for each wr_route no-lock where wr_lot = wo_lot:
         if wr_po_nbr = pod_nbr and wr_po_line = pod_line then do:
            wopart = wo_part.
            leave.
         end.
      end.
   end.

   if wopart = "" then leave.

   /* Note:  Scheduled orders should always appear before regular order in    */
   /*        order of due date. If the only regular orders (wo_type = "") are */
   /*        MRP planned orders, and no orders or schedules have been changed */
   /*        since the last time MRP was run, things should fall out in order */
   /*        of their due dates                                               */

   do i = 1 to 2:

      if i = 1 then
         assign
            wotype = "s"
            header_desc =
               getTermLabel("REPETITIVE_SCHEDULE_OP_DATA_SELECTD",45).
      else
         assign
            wotype = ""
            header_desc =
               getTermLabel("WORK_ORDER_OPERATION_DATA_SELECTED",45).

      {gprun.i ""rssupa1.p""
                "(input wotype,
                  input wopart,
                  input pod_site,
                  input recid(pod_det),
                  input start_date,
                  input asof_date,
                  input report_yn,
                  input header_desc,
                  input-output warn_ct)"}
   end.

   /*IF THERE IS A FIRM INTERVAL, THEN WE NEED TO  GO  BACK  THRU
   THE  WORKFILE  AND  REDUCE  WORKFILE  RECORDS  IN THE OLD FIRM
   INTERVAL BY THE NET FIRM QUANTITIES, AND SEE IF ANYTHING  LEFT
   THAT  WE  NEED  TO WARN ABOUT.  THIS IS BECAUSE MRP DOESN'T DO
   ANYTHING  WITH  OPERATION  "SUPPLY",  SO  WE  NEED  TO  DO  IT
   OURSELVES.   I  DIDNT  WANT TO DO IT THIS WAY BECAUSE IT SEEMS
   KIND OF KLUGEY; I REALIZE THERE IS A MUCH BETTER WAY TO DO ALL
   THIS  IF THE UNDERLYING DESIGN COULD BE REFINED AND BASED ON A
   PROCESS MODEL RATHER THAN THE BOM/ROUTING MODEL.*/

   if start_date <> ? then do:
      /*NET OFF ANY "NET PRIOR OPEN" QTY*/

      find sch_mstr where sch_type = supp_schedule
         and sch_nbr = pod_nbr
         and sch_line = pod_line
         and sch_rlse_id = pod_curr_rlse_id[supp_schedule - 3]
         no-lock.

      assign
         open_qty = max(sch_pcr_qty - pod_cum_qty[1], 0)
         qty_left = open_qty.

      for each work_schd
            where schd_date <= sch_pcs_date,
            each wo_mstr no-lock
            where wo_lot = work_schd.schd__chr01
            while qty_left > 0
            by schd_date by schd_time with frame f-b:

         setFrameLabels(frame f-b:handle).

         qty_to_apply = min(qty_left,schd_discr_qty).

         FORM /*GUI*/  header
            skip(2)
            l-msg-opr-data +
               getTermLabel("SCHEDULE_PRIOR_CUM_REQD_OPEN_QTY",45)
               + " " + string(open_qty) format "x(120)"
            skip(1)
         with STREAM-IO /*GUI*/  width 132 no-box.

         if report_yn then
            display
               wo_lot
               wo_type
               wo_status
               work_schd.schd__dec01 format ">>>>>9" @ wr_route.wr_op
               work_schd.schd_discr_qty       @ wo_qty_ord
                  column-label {&rssupa_p_9}
               work_schd.schd_date            @ arrive_date
                  column-label {&rssupa_p_17}
               qty_to_apply column-label {&rssupa_p_13}
                WITH STREAM-IO /*GUI*/ .

         schd_discr_qty = schd_discr_qty - qty_to_apply.
         if schd_discr_qty = 0 then delete work_schd.
         qty_left = qty_left - qty_to_apply.
      end.

      /*NET OFF FIRM SCHEDULE NET OPEN QTIES*/

      FORM /*GUI*/  header
         skip(2)
         l-msg-opr-data-open no-label
         skip(1)
      with STREAM-IO /*GUI*/  frame d1-head width 132 no-box down.

      for each schd_det no-lock
            where schd_type = sch_type
            and schd_nbr = sch_nbr
            and schd_line = sch_line
            and schd_rlse_id = sch_rlse_id
            and schd_date < start_date
            and schd_fc_qual = "f":
         assign
            open_qty =
               max(min(schd_discr_qty, schd_cum_qty - pod_cum_qty[1]),0)
            qty_left = open_qty.

         for each work_schd
               where work_schd.schd_date = schd_det.schd_date
               and work_schd.schd_time = schd_det.schd_time,
               each wo_mstr no-lock
               where wo_lot = work_schd.schd__chr01
               while qty_left > 0 with frame d1:
            setFrameLabels(frame d1:handle).

            qty_to_apply = min(qty_left,work_schd.schd_discr_qty).

            if report_yn then do:
               display
                  wo_lot
                  wo_type
                  wo_status
                  work_schd.schd__dec01 format ">>>>>9" @ wr_route.wr_op
                  work_schd.schd_discr_qty       @ wo_qty_ord
                     column-label {&rssupa_p_9}
                  work_schd.schd_date            @ arrive_date
                     column-label {&rssupa_p_17}
                  open_qty column-label {&rssupa_p_12}
                  qty_to_apply column-label {&rssupa_p_13}
               with frame d1 STREAM-IO /*GUI*/ .

               down 1 with frame d1.
            end.

            work_schd.schd_discr_qty = work_schd.schd_discr_qty - qty_to_apply.
            if work_schd.schd_discr_qty = 0 then delete work_schd.
            qty_left = qty_left - qty_to_apply.
         end.
      end.

      for each work_schd exclusive-lock
            where work_schd.schd_date < start_date,
            each wo_mstr no-lock
            where wo_lot = work_schd.schd__chr01 with frame f-a:

         setFrameLabels(frame f-a:handle).

         warn_ct = warn_ct + 1.

         FORM /*GUI*/  header
            skip(2)
            l-msg-opr-data-del + " " +
               string(start_date) format "x(75)" no-label
            skip(1)
         with STREAM-IO /*GUI*/  width 132 no-box.

         display
            wo_lot
            wo_type
            wo_status
            work_schd.schd__dec01 format ">>>>>9" @ wr_route.wr_op
            work_schd.schd_discr_qty       @ wo_qty_ord
               column-label {&rssupa_p_9}
            work_schd.schd_date            @ arrive_date
               column-label {&rssupa_p_17}
            l-msg-order-data no-label format "x(50)" WITH STREAM-IO /*GUI*/ .

         delete work_schd.
      end.
   end.
end.
